package com.liugh.service.zhongche.Imp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.entity.standard.StandardPark;
import com.liugh.mapper.zhongche.StandardParkMapper;
import com.liugh.model.DownloadModel;
import com.liugh.service.IUserService;
import com.liugh.service.zhongche.ISearchService;
import com.liugh.service.zhongche.IStandardParkService;
import com.liugh.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Fatao on 2018/11/5 .
 */

@Service
public class StandardParkServiceImpl implements IStandardParkService {


    @Autowired
    private StandardParkMapper standardParkMapper;

    @Autowired
    ISearchService searchService;

    @Autowired
    IUserService userService;


    /**
     * 新增一条park .
     * @param standardPark .
     * @return .
     */
    public StandardPark addPark(StandardPark standardPark) {
        return CommonUtil.isDBOpSuccess(standardParkMapper.insert(standardPark)) ? standardPark : null;
    }


    /**
     * 更新park .
     * @param standardPark .
     * @return .
     */
    public StandardPark updatePark(StandardPark standardPark) {
        return CommonUtil.isDBOpSuccess(standardParkMapper.insert(standardPark)) ? standardPark : null;
    }


    /**
     * 删除一条记录 .
     * @param id .
     * @return .
     */
    public int delParkById(int id) {
        return standardParkMapper.deleteById(id);
    }







    /**
     * 根据id查找 .
     * @param id .
     * @return .
     */
    public StandardPark getParkById(int id) {
        return standardParkMapper.selectById(id);
    }


    /**
     * 分页获取所有的park .
     * @param page .
     * @return .
     */
    public Page<StandardPark> getAllPark(Page<StandardPark> page) {
        List<StandardPark> list = standardParkMapper.selectPage(page, new EntityWrapper<StandardPark>().orderBy("create_time", false));
        for (StandardPark standardPark : list) {
            standardPark.setAuthorName(userService.getUserByUserNo(standardPark.getAuthor()).getUsername());
        }
        return page.setRecords(list);
    }




    /**
     * 分页获取我创作的park .
     * @param page .
     * @param userNo .
     * @return .
     */
    public Page<StandardPark> getMyPark(Page<StandardPark> page,String  userNo) {
        return page.setRecords(standardParkMapper.selectPage(page, new EntityWrapper<StandardPark>().eq("author", userNo)));
    }

    /**
     * 下载附件时返回文件的URL和原始文件名
     * @param id
     * @return
     */
    @Override
    public DownloadModel getPathAndFilename(int id) {
        StandardPark park = standardParkMapper.selectById(id);
        String path  = park.getAttachment();
        String name = searchService.getOriginName(CommonUtil.getFileNameByUrl(park.getAttachment()));
        if (!(StrUtil.isEmpty(path) && StrUtil.isEmpty(name))) {
            return new DownloadModel(path,name);
        }
        return null;
    }


}
